package {
	
	import common.MapData;
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.getTimer;
	
	import globals.Global;
	import globals.Setting;
	
	import net.message.CGUserMessage;
	
	import ui.scene.BaseScene;
	
	public class Game extends Sprite {
		
		public var sceneLayer:Sprite;
		public var unitLayer:Sprite;
		public var faceLayer:Sprite;
		public var panelLayer:Sprite;
		public var topLayer:Sprite;

		public function Game() {
			sceneLayer = new Sprite();
			sceneLayer.mouseEnabled = sceneLayer.tabEnabled = sceneLayer.tabChildren = false;
			addChild(sceneLayer);
			unitLayer = new Sprite();
			unitLayer.mouseEnabled = unitLayer.tabEnabled = unitLayer.tabChildren = false;
			addChild(unitLayer);
			faceLayer = new Sprite();
			faceLayer.mouseEnabled = faceLayer.tabEnabled = faceLayer.tabChildren = false;
			addChild(faceLayer);
			panelLayer = new Sprite();
			panelLayer.mouseEnabled = panelLayer.tabEnabled = panelLayer.tabChildren = false;
			addChild(panelLayer);
			topLayer = new Sprite();
			topLayer.mouseEnabled = topLayer.tabEnabled = topLayer.tabChildren = false;
			addChild(topLayer);
		}
		
		public function start(stage:Stage):void {
			Global.init(stage, this);
			
//			parseMap();
//			parseFighter();
//			parseMagic();
			
//			Global.serviceManager.send(new CGUserMessage());
			initGame();
		}
		
		public function initGame():void {
			Global.sceneManager.enter(BaseScene.GAME_SCENE);
			Global.stage.addEventListener(Event.ENTER_FRAME, frameHandler);
			
			var mapData:MapData = new MapData();
			var n:uint = Setting.TILE_WIDTH;
			var path:Array = [
				[0,0], [1*n,0], [1*n, 1*n], [2*n, 1*n], [2*n, 2*n], [3*n, 2*n], [3*n, 3*n], [4*n, 3*n], [4*n, 4*n],
				[5*n, 4*n], [5*n, 5*n], [6*n, 5*n], [7*n, 5*n], [8*n, 5*n], [8*n, 6*n], [9*n, 6*n], [10*n, 6*n],
			];
			mapData.path = path;
			mapData.tiles = [
				["map_1", "map_2", "map_1", "map_2", "map_1", "map_2", "map_1", "map_2", "map_1", "map_2"],
				["map_2", "map_1", "map_2", "map_1", "map_2", "map_1", "map_2", "map_1", "map_2", "map_1"],
				["map_1", "map_2", "map_1", "map_2", "map_1", "map_2", "map_1", "map_2", "map_1", "map_2"],
				["map_2", "map_1", "map_2", "map_1", "map_2", "map_1", "map_2", "map_1", "map_2", "map_1"],
				["map_1", "map_2", "map_1", "map_2", "map_1", "map_2", "map_1", "map_2", "map_1", "map_2"],
				["map_2", "map_1", "map_2", "map_1", "map_2", "map_1", "map_2", "map_1", "map_2", "map_1"],
				["map_1", "map_2", "map_1", "map_2", "map_1", "map_2", "map_1", "map_2", "map_1", "map_2"]
			];
			Global.mapManager.initMap(mapData);
			Global.unitManager.addTest();
			Global.battleManager.startWave();
		}
		
		private function parseMap():void {
			var mapData:BitmapData = Setting.ASSETS["resources/map.swf"];
			var tile:BitmapData;
			var rect:Rectangle = new Rectangle(0, 0, Setting.TILE_WIDTH, Setting.TILE_HEIGHT);
			var dest:Point = new Point();
			for (var i:int = 0; i<mapData.width / Setting.TILE_WIDTH; i++) {
				for (var j:int = 0; j<mapData.height / Setting.TILE_HEIGHT; j++) {
					tile = new BitmapData(Setting.TILE_WIDTH, Setting.TILE_HEIGHT, true, 0xFFFFFF);
					rect.x = i * Setting.TILE_WIDTH;
					rect.y = j * Setting.TILE_HEIGHT;
					tile.copyPixels(mapData, rect, dest);
					Global.assetsManager.addAssets("map_" + i + "_" + j, tile);
				}
			}
		}
		
		private function frameHandler(event:Event):void {
			Global.timeStamp = getTimer();
			Global.unitManager.update();
		}
	}
}